package com.example.myxmpp.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myxmpp.R;
import com.example.myxmpp.service.IMService;
import com.example.myxmpp.service.PushService;
import com.example.myxmpp.utils.ThreadUtils;

import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jxmpp.stringprep.XmppStringprepException;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;


public class LoginActivity extends AppCompatActivity {


    public static final String SERVICENAME = "long-pc";//"itheima.com";192.168.0.102
    private TextView etUserName;
    private TextView etPassWord;
    private Button mBtnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);



        initView();
        initListener();


    }

    private void initListener() {
        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断
                final String userName = etUserName.getText().toString();
                final String passWord = etPassWord.getText().toString();

                if (TextUtils.isEmpty(userName)){//用户名为空
                    etUserName.setError("用户名不能为空");
                    return;
                }

                if (TextUtils.isEmpty(passWord)){//密码为空
                    etPassWord.setError("密码不能为空");
                    return;
                }

                final String ym = "192.168.0.102";

                ThreadUtils.runInThread(new Runnable() {
                    @Override
                    public void run() {
                        //开始创建连接以像
                        Log.i("log","===================ThreadUtils开始创建连接以像=======");

                        try {
                            XMPPTCPConnectionConfiguration configuration = XMPPTCPConnectionConfiguration.builder().setXmppDomain(ym)// 设置域名
                                    .setHostAddress(InetAddress.getByName(ym))// 设置主机
                                    .setPort(5222)
                                    .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)// 禁用SSL连接
                                    .setCompressionEnabled(false)
                                    .build();

                            // 设置需要经过同意才可以添加好友
                            Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);

                            AbstractXMPPConnection conn = new XMPPTCPConnection(configuration);
                            conn.connect();// 连接, 可设置监听

                            conn.login(userName,passWord);

                            ThreadUtils.runInUIThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(getApplicationContext(),"成功登录",Toast.LENGTH_SHORT).show();
                                }
                            });

                            //跳到主界面
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);

                            //需要保存连接对像
                            IMService.conn = conn;


                            //保存当前登录的账户
                           String account = userName +"@"+LoginActivity.SERVICENAME;
                            IMService.mCurAccount = account;//xxxx@xxx.com

                            //启动IMService
                            Intent service = new Intent(LoginActivity.this,IMService.class);
                            startService(service);

                            //启动PushService
                            Intent pushService = new Intent(LoginActivity.this, PushService.class);
                            startService(pushService);


                        } catch (XmppStringprepException e) {
                            e.printStackTrace();
                        } catch (UnknownHostException e) {
                            e.printStackTrace();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (SmackException e) {
                            e.printStackTrace();
                        } catch (XMPPException e) {
                            e.printStackTrace();
                            ThreadUtils.runInUIThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(getApplicationContext(),"登录失败",Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    }
                });

        }
        });
    }

    private void initView() {
        etUserName = findViewById(R.id.et_username);
        etPassWord = findViewById(R.id.et_pssword);

        mBtnLogin = findViewById(R.id.btn_login);



    }
}
